package com.jdy.haoduoaiteacher.adapter;

import android.annotation.SuppressLint;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.TextAppearanceSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.jdy.haoduoaiteacher.activity.BaseActivity;
import com.ycsj.goldmedalnewconcept.R;

import java.util.ArrayList;

@SuppressLint("InflateParams")
public class KeyListAdapter extends BaseAdapter {

  private BaseActivity mContext;
  private ArrayList<String> mList;
  private String mKey;
  private int keyLength;
  private LayoutInflater mInflater;

  public KeyListAdapter(BaseActivity Context, ArrayList<String> list, String key) {
    this.mContext = Context;
    this.mList = list;
    this.mKey = key;
    mInflater = LayoutInflater.from(mContext);
    keyLength = mKey.length();
  }

  @Override
  public int getCount() {
    return mList.size();
  }

  @Override
  public Object getItem(int arg0) {
    return mList.get(arg0);
  }

  @Override
  public long getItemId(int arg0) {
    return arg0;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    String content = mList.get(position);
    ViewHolder holder;
    if (convertView == null) {
      holder = new ViewHolder();
      convertView = mInflater.inflate(R.layout.listkeyitem, null);
      holder.content = (TextView) convertView.findViewById(R.id.content);
      convertView.setTag(holder);
    } else {
      holder = (ViewHolder) convertView.getTag();
    }
    SpannableString styledText = new SpannableString(content);

    if (content.indexOf(mKey) >= 0) {
      styledText.setSpan(new TextAppearanceSpan(mContext, R.style.stylec),
          content.indexOf(mKey), content.indexOf(mKey) + keyLength,
          Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }

    // styledText.setSpan(new TextAppearanceSpan(mContext, R.style.styled),
    // keyLength, content.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    holder.content.setText(styledText, TextView.BufferType.SPANNABLE);
    return convertView;
  }

  private static class ViewHolder {
    private TextView content;
  }

}
